<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Workarea extends CI_Controller {


	var $_login_userid;
	
	function __construct()
	 {
	  
		parent::__construct();
		//$this->_login_userid=0;
		$this->_login_userid=$this->get_login_userid();
	 }

	private function get_login_userid(){
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		if ($this->tank_auth->is_logged_in()){
			return $this->tank_auth->get_user_id();
		}else{
			return 0;
		}
	}
	
	public function index()
	{
		$this->load->helper('url');
		$this->load->view('welcome_message');
	}
	public function checklogin()
	{
		
		if($this->_login_userid==$_POST['uid']){
			$result_arr = array(
               'status'=> 1,
			   'message'=>'保持联系！',
			   'uid'=> $this->_login_userid
            );
		}else{
			$result_arr = array(
               'status'=> 0,
			   'message'=>'你未登录或已经过期了，请登录！',
			   'uid'=> $this->_login_userid
            );
		}
		$json_string = json_encode($result_arr);    
		echo $json_string;
	}
	
	public function child($parent){
		$this->load->database();
//		$this->db->cache_on();
		$this->db->where('parent', 0); 
		
		$this->db->where('uid', $this->_login_userid);
		$query = $this->db->get('task');
		$data['result'] = $query;
		$this->load->view('home',$data);
	}

	public function insert(){
		
			$newnode_sort=time();
			$this->load->database();
			$sql = "INSERT INTO task (title, parent,sort,in_date,uid) 
			VALUES (".$this->db->escape($_REQUEST['title']).", ".$this->db->escape($_REQUEST['parent']).",'".$newnode_sort."','".date("Y-m-d H:m:s")."','". $this->_login_userid."')";
			$this->db->query($sql);
			if($this->db->affected_rows()>0){
				$status=1;
			}else{
				$status=0;
			}
			$result_arr = array(
				   'status'=> $status,
				   'message'=>'消息'.$this->db->last_query(),
				   'data' => array(
						'newid'=>$this->db->insert_id(),
						'newnode_sort'=>$newnode_sort
				   )
				);
				
			if($this->_login_userid==0){
				$result_arr['status'] = 0;
				$result_arr['message'] = '你未登录或已经过期了，新增的任务将不属于你私有，请先登录！';
			}		
			
		$json_string = json_encode($result_arr);    
		echo $json_string; 		
}
	
	public function change_parent(){
		$this->load->database();
//		$this->db->set('parent', $_POST['new_parent']); 
		$data = array(
               'parent' => $_POST['new_parent']
            );
		$this->db->where('id',$_POST['taskid']);
		$this->db->where('uid', $this->_login_userid);
		$this->db->update('task', $data);
		
//		$sql = "UPDATE task SET parent = ".$this->db->escape($_POST['new_parent'])." WHERE id=".$_POST['taskid'].";";
//		$this->db->query($sql);
		
		echo   $_POST['taskid'],' TO ',$_POST['new_parent'];
		echo $this->db->last_query();
		echo $this->db->affected_rows();
	}
	
	public function edit(){
		$this->load->database();
		$data = array(
               'title' => $_POST['title']
            );
		$this->db->where('id',$_POST['id']);
		$this->db->where('uid', $this->_login_userid);
		$this->db->update('task', $data);
//		echo   $_POST['taskid'],' TO ',$_POST['new_parent'];
//		echo $this->db->last_query();
//		echo $this->db->affected_rows();
	}
	
	public function editmore(){
		$this->load->database();
		$data = array(
               'comment' => $_POST['comment'],
			   'nodetype'=> $_POST['nodetype'],
			   'link'=> $_POST['link']
            );
		$this->db->where('id',$_POST['id']);
		$this->db->where('uid', $this->_login_userid);
		$this->db->update('task', $data);
//		echo   $_POST['taskid'],' TO ',$_POST['new_parent'];
//		echo $this->db->last_query();
//		echo $this->db->affected_rows();
	}	
	public function change_stage(){
		$this->load->database();
		$data = array(
               'stage' => $_POST['stage']
            );
		$this->db->where('id',$_POST['id']);
		$this->db->where('uid', $this->_login_userid);
		$this->db->update('task', $data);
//		echo   $_POST['taskid'],' TO ',$_POST['new_parent'];
//		echo $this->db->last_query();
//		echo $this->db->affected_rows();
	}	
	
	public function change(){
		$this->load->database();
		$data = array(
               $_POST['parm1'] => $_POST['value1']
            );
		$this->db->where('id',$_POST['id']);
		$this->db->where('uid', $this->_login_userid);
		$this->db->update('task', $data);
		if($this->db->affected_rows()>0){
			$status=1;
		}else{
			$status=0;
		}
		$result_arr = array(
               'status'=> $status,
			   'message'=>'消息'.$this->db->last_query()
			   );
			$json_string = json_encode($result_arr);    
			echo $json_string; 
	}
	
	public function changeSort2(){
		$this->load->database();
		$data = array(
               'sort' => $_POST['sort1']
            );
		$this->db->where('id',$_POST['id1']);
		$this->db->where('uid', $this->_login_userid);
		$this->db->update('task', $data);
		$query1=$this->db->affected_rows();
		$message1="step1:".$query1.",".$this->db->last_query();
//		echo $message1;
//		echo $query1;
		
		$data = array(
               'sort' => $_POST['sort2']
            );
		$this->db->where('id',$_POST['id2']);
		$this->db->where('uid', $this->_login_userid);
		$this->db->update('task', $data);
		$query2=$this->db->affected_rows();
		$message2="step2:".$query2.",".$this->db->last_query();
//		echo $message2;
//		echo $query2;
		
		if(($query1+$query2)>0){
			$status=1;
		}else{
			$status=0;
		}
		$result_arr = array(
               'status'=> $status,
			   'message'=>'消息'.$message1.$message2,
			   );
			$json_string = json_encode($result_arr);    
			echo $json_string; 
	
	
	
	}
	
	public function set_parm_null(){
		$this->load->database();
		$sql='UPDATE task SET '.$_POST['parm1'].'=NULL WHERE id='.$_POST['id'].';';
//		echo $sql;
		$query = $this->db->query($sql);
		if($this->db->affected_rows()>0){
			$status=1;
		}else{
			$status=0;
		}
		$result_arr = array(
               'status'=> $status,
			   'message'=>'消息'.$this->db->last_query()
			   );
			$json_string = json_encode($result_arr);    
			echo $json_string;  
	} 
	
	public function del(){
		$taskid=$_POST['id'];
		$this->load->database();
		$this->db->where('parent',$taskid ); 
		$query = $this->db->get('task');
		if ($query->num_rows() > 0)
		{
            $result_arr = array(
               'status'=> 0,
			   'message'=>'该节点还有子结点，不能被删除!'
			   );
		}else{	
			$query->free_result(); 
            $sql = "DELETE FROM task WHERE id=".$taskid.";";
			$this->db->query($sql);
            if($this->db->affected_rows()>0){
                $result_arr = array(
                   'status'=> 1,
                   'message'=>'已成功删除!'
			   );
            }else{
                $result_arr = array(
                   'status'=> 0,
                   'message'=>'出错了：'.$this->db->last_query()
			   );
            }
		}
        $json_string = json_encode($result_arr);    
        echo $json_string; 
	}
	
	public function getlist(){
		$this->load->database();
		$this->db->where('uid', $this->_login_userid);
//		$this->db->order_by('sort','desc');
		$query = $this->db->get('workarea');
		if ($query->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query->result_array() as $row)
			{
				$rows_arr[]=$row;
			}
//			print_r($rows_arr);
			$json_string = json_encode($rows_arr);    
			echo $json_string;  
		} 
	}

	public function active(){
		$this->load->database();
		$this->db->where('active', 1); 
		$this->db->where('uid', $this->_login_userid);
		$this->db->order_by('sort','desc');
		$query = $this->db->get('task');
		if ($query->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query->result_array() as $row)
			{
//				echo $row['id'];
				$rows_arr[$row['id']]=$row;
			}
//			print_r($rows_arr);
			$json_string = json_encode($rows_arr);    
			echo $json_string;  
		} 
	}
	
	public function get_attention(){
		$this->load->database();
		$this->db->where("attention > 0 "); 
		$this->db->where('uid', $this->_login_userid);
		$this->db->order_by('attention','desc');
		$query = $this->db->get('task');
		if ($query->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query->result_array() as $row)
			{
//				echo $row['id'];
				$rows_arr[$row['id']]=$row;
			}
//			print_r($rows_arr);
			$json_string = json_encode($rows_arr);    
			echo $json_string;  
		} 
	}

	public function getnode($id,$sys=0){
		$this->load->database();	
		$this->db->where('id', $id); 
//		$this->db->where('uid', $this->_login_userid);
		$query1 = $this->db->get('task');
		

		if ($query1->num_rows() > 0)
		{
		    $row = $query1->row_array(); 
		    $query1->free_result(); 
//			print_r($row);
			if($row['nodetype']=='S'){
				$query = $this->db->get('task');
				$where='';
				if($row['tags']){					
					$tags=explode(' ', trim($row['tags']));  
					foreach ($tags as $tag){
						if($tag){
							$where.=' AND (tags LIKE "%'.$tag.'%" OR title LIKE "%'.$tag.'%") ';
						}
					}	
				}
				if($row['stage']){
					$where.=' AND stage="'.$row['stage'].'" ';
				}
				if($where){
					$where='1 AND id<>'.$row['id'].$where;
				}else{
					return; //防止出现空查询条件
				}
//				echo $where;
				
				$this->db->where($where,NULL,FALSE);
				$getway="S";
			}else{
				$this->db->where('parent', $id); //默认查询该ID的子节点
				$getway='P';//记录节点取得的方式
			}
		}
		if($sys==1){			
			$this->db->where('uid', 3);
		}else{
			$this->db->where('uid', $this->_login_userid);
		}
		
		$this->db->order_by('sort desc,in_date desc');
		$query2 = $this->db->get('task');
		if ($query2->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query2->result_array() as $row)
			{
//				$rows_arr[$row['id']]=$row;
				$row['getway']=$getway;
				$rows_arr[]=$row;
			}
			$json_string = json_encode($rows_arr);    
			echo $json_string;
		} 		 
	}
	
	public function getnode_sys($id){
		$this->getnode($id,1);
	
	}
	
	private function get_by_where($where){
		$this->load->database();	
		if($where){
		}else{//防止出现空查询条件
			$row[id]="10";
			$row['title']="出错了，不能进行空查询！";
			$rows_arr[]=$row;
			$json_string = json_encode($rows_arr);    
			echo $json_string;
		}
//		echo $where;		
		$this->db->where('uid', $this->_login_userid);
		$this->db->where($where,NULL,FALSE);
		$getway="S";//记录节点取得的方式
		$this->db->order_by('sort','desc');
		$query = $this->db->get('task');
		if ($query->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query->result_array() as $row)
			{
//				$rows_arr[$row['id']]=$row;
				$row['getway']=$getway;
				$rows_arr[]=$row;
			}
			$json_string = json_encode($rows_arr);    
			echo $json_string;
		} 		
	}
	
	public function search_by_post_parm(){
		$where=" 1 ";
		if($_REQUEST['active']){
			$where.=" AND active=".$this->db->escape($_REQUEST['active'])." ";
		}
		if($_REQUEST['parent']){
			$where.=" AND parent=".$this->db->escape($_REQUEST['parent'])." ";
		}
		if($_REQUEST['stage']===null){
		}else{
			$where.=" AND stage=".$this->db->escape($_REQUEST['stage'])." ";
		}
//		echo $where;
		$this->get_by_where($where);
		
	}
	
	public function search_by_stage($stage){
//		$where=" stage='".$stage."' ";
		$where=" active=1 AND stage='".$stage."' ";
		$this->get_by_where($where);
	}
	
	
	public function search_by_text(){
		$result_arr = array();
		$this->load->database();	
		$where='';
		$search_words=explode(' ', trim($_POST['search_words'])); 
		foreach ($search_words as $word){
			if($word){
				$where.=' AND (tags LIKE "%'.$word.'%" OR title LIKE "%'.$word.'%") ';
			}
		}
		if($where){
			$where='1'.$where;
		}else{
			$result_arr['status']='error';
			$result_arr['message']='不能执行一个空查询！';
			$json_string = json_encode($result_arr);    
			echo $json_string; 
			return;//防止出现空查询条件
		}
//		echo $where;		
		$this->db->where('uid', $this->_login_userid);
		$this->db->where($where,NULL,FALSE);
		$getway="S";//记录节点取得的方式
		$this->db->order_by('sort','desc');
		$query = $this->db->get('task');
		if ($num_rows=$query->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query->result_array() as $row)
			{
//				$rows_arr[$row['id']]=$row;
				$row['getway']=$getway;
				$rows_arr[]=$row;
			}
			$result_arr['data']=$rows_arr;
			$result_arr['status']='success';
			$result_arr['message']='共找到'.$num_rows.'个结果。';
		}else{
			$result_arr['status']='noresult';
			$result_arr['message']='没有匹配的结果！';		
		}
		$json_string = json_encode($result_arr);    
		echo $json_string; 
	}
	
	public function get($id){
		$this->load->database();
		$this->db->where('id', $id); 
		$query = $this->db->get('task');
		if ($query->num_rows() > 0)
		{
		    $row = $query->row_array(); 
			$json_string = json_encode($row);    
			echo $json_string;  
		} 
//		print_r($row);
	}
	private function get_parents_array($id){
		$this->load->database();
		$parents_array =array();
		$opt=$id;
		do{
			$this->db->where('id', $opt); 
			$query = $this->db->get('task');
//			echo $this->db->last_query();
			if ($query->num_rows() > 0)
			{
				$row = $query->row_array(); 
//				$result_message=var_export($row,true);
				$parents_array[] =$row['id'];
				$opt=$row['parent'];
				continue;
			}else{
				$opt=NULL;
				break;
			}
		}while($opt);
		return array_reverse($parents_array);
	}
	
	private function array_to_json($array){
		$json_string = json_encode($array);    
		echo $json_string; 
	}
	public function getall(){
		$this->load->database();
		if($this->_login_userid){
			$this->db->where('uid', $this->_login_userid);
		}else{
			$this->db->where('uid', 5);
		}
		
		$this->db->where('stage !=', 'finish');//默认只取得所有未完成的任务
		$this->db->order_by('parent asc,sort desc');
		$query = $this->db->get('task');
		if ($num_rows=$query->num_rows() > 0)
		{
//			$result_array['message']=$result_message;
			$result_array['data']=$query->result_array();
			$this->array_to_json($result_array);
		}
	}
	
	public function gethelp(){
		$this->load->database();
		$this->db->where('uid', 3);
//		$this->db->where('stage !=', 'finish');//默认只取得所有未完成的任务
		$this->db->order_by('parent asc,sort desc');
		$query = $this->db->get('task');
		if ($num_rows=$query->num_rows() > 0)
		{
			$result_array['data']=$query->result_array();
			$this->array_to_json($result_array);
		}
	}
	
	public function getparents($id){
		$this->load->database();
		$parents_array =array();
		$opt=$id;
		do{
			$this->db->where('id', $opt); 
			$query = $this->db->get('task');
//			echo $this->db->last_query();
			if ($query->num_rows() > 0)
			{
				$row = $query->row_array(); 
				$result_message=var_export($row,true);
				$parents_array[] =$row;
				$opt=$row['parent'];
				continue;
			}else{
				$opt=NULL;
				break;
			}
		}while($opt);
		$result_array['message']=$result_message;
		$result_array['data']=array_reverse($parents_array);
		$json_string = json_encode($result_array);    
		echo $json_string; 		

	}
	public function getparents_by_tag(){
		$all=array();
		$this->load->database();	
		$where='';
		$search_words=explode(' ', trim($_POST['search_words']));
// $search_words=explode(' ', trim('默认打开'));
		foreach ($search_words as $word){
			if($word){
				$where.=' AND (tags LIKE "%'.$word.'%") ';
			}
		}
		if($where){
			$where='1'.$where;
		}else{
			
			return;//防止出现空查询条件
		}
//		echo $where;		
		$this->db->where('uid', $this->_login_userid);
		$this->db->where($where,NULL,FALSE);
		$getway="S";//记录节点取得的方式

		$query = $this->db->get('task');
		if ($query->num_rows() > 0)
		{
			$rows_arr=array();
			$rows_arr=$query->result_array();
			foreach ($rows_arr as $row)
			{
				$all=array_merge($all,$this->get_parents_array($row['id']));
			}
			$all=array_unique($all);
			$json_string = json_encode($all);    
			echo $json_string;
		} 		 
	
	}
	
	public function get_task_for_cal(){
		$this->load->database();
		$sql=('SELECT id, title, start_date AS start,allday FROM task WHERE start_date IS NOT NULL AND stage<>"finish" AND uid='.$this->_login_userid.';');
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query->result_array() as $row)
			{
//				echo $row['id'];
				$rows_arr[]=$row;
			}
//			print_r($rows_arr);
			$json_string = json_encode($rows_arr);    
			echo $json_string;  
		} 
	}
	
	
}

